package com.maimao.info.controller;

import com.maimao.api.mall.info.IntegralActionApi;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.Result;
import com.maimao.info.controller.base.BaseController;
import com.maimao.info.service.IntegralActionService;
import com.maimao.model.info.IntegralAction;
import io.seata.core.context.RootContext;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

/**
 * 积分日志
 *
 * @author MaoLin Wang
 * @date 2021/1/21 4:23 下午
 */
@RestController
public class IntegralActionController extends BaseController implements IntegralActionApi {

    @Autowired
    private IntegralActionService integralActionService;

    /**
     * 保存积分日志
     */
    @Override
    @PostMapping("/client/addBatch")
    public Result<?> saveBatch(@RequestBody List<IntegralAction> integralActionList) {
        if (integralActionList == null || integralActionList.size() <= 0) {
            return Result.notValid();
        }
        String xid = getRequest().getHeader(RootContext.KEY_XID);
        if (StringUtils.isNotEmpty(xid)) {
            RootContext.bind(xid);
        }
        integralActionService.addBatch(integralActionList);
        return Result.ok();
    }

    /**
     * 用户积分轨迹
     */
    @GetMapping("/action-data")
    @Override
    public Result<?> userIntegralActionData() {
        AuthUser authUser = checkAndGetAuthUser();
        List<IntegralAction> actionList = integralActionService.userIntegralActionData(authUser.getId2Long());
        return Result.okData(actionList);
    }
}
